import vim
from pyparsing import *


def convertAtomAction(s,l,t):
    if t[0] == "num" : 
        return "[0-9]"
    if t[0] == "alpha" :
        return "[a-zA-Z]"
    if t[0] == "hexnum" :
        return "[a-fA-F0-9]"
    if t[0] == "alphanum" :
        return "[0-9a-zA-Z]"
    if t[0] == "wordstart" :
        return r"\b"
    if t[0] == "wordend" :
        return r"\b"
    if t[0] == "linestart" :
        return r"^"
    if t[0] == "lineend" :
        return r"$"

def convertMultiAction(s,l,t):
    pass

def getSyntax():
    NumToken = Keyword("num",caseless = True )
    AlphaToken = Keyword("alpha",caseless = True)
    HexnumToken = Keyword("hexnum", caseless = True)
    AlphanumToken = Keyword("alphanum", caseless = True)
    WordstartToken = Keyword("wordstart", caseless = True)
    WordendToken = Keyword("wordend", caseless = True)
    LinestartToken = Keyword("linestart", caseless = True)
    LineendToken = Keyword("lineend" , caseless = True )
    numToken=Word(nums)

    Atom = (NumToken | AlphaToken | HexnumToken | AlphanumToken | WordstartToken | \
            WordendToken | LinestartToken | LineendToken).setParseAction(convertAtomAction)

    Multi = numToken | numToken + Keyword("to") + numToken | numToken + Keyword("or") | \
            Keyword("at") + Keyword("least") + numToken 

    Piece = Atom + Optional(Multi)

    return Piece


if __name__ == "__main__" :
    syntax = getSyntax()
    exp =  "alpha sdf"
    result = syntax.transformString(exp)
    print result


